package com.yanggeng.demo.beginnerguidedemo.activity;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;

import com.yanggeng.demo.beginnerguidedemo.R;
import com.yanggeng.demo.beginnerguidedemo.adapter.SimpleAdapter;
import com.yanggeng.demo.beginnerguidedemo.utils.DeviceParams;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class ScrolledRecylerViewActivity extends BaseActivity {

    @BindView(R.id.rv_common)
    RecyclerView mRvCommon;
    @BindView(R.id.sv_main)
    ScrollView mSvMain;

    private LinearLayoutManager mLinearLayoutManager;
    private Context mContext;
    private ScrolledAdapter mAdapter;
    List<String> data = new ArrayList<>();

//    private HeaderRecycleAdapter mNormalAdapter;

    private int mSumCount = 10;
    private int mMiddlePosition = 0;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recyclerview);
        ButterKnife.bind(this);
        setTitle(true, "RecyclerView");
        mContext = this;
        mLinearLayoutManager = new LinearLayoutManager(this, OrientationHelper.VERTICAL, false) {
            @Override
            public boolean canScrollHorizontally() {
                return false;
            }

            @Override
            public boolean canScrollVertically() {
                return false;
            }
        };
        for (int i = 0; i < 3; i++) {
            data.add(String.valueOf(i));
        }
        mAdapter = new ScrolledAdapter(mContext, data);
        mRvCommon.setLayoutManager(mLinearLayoutManager);
        mRvCommon.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new SimpleAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {
                if (position == mMiddlePosition) {
                    mMiddlePosition++;
                    if (position != 0 && position < mSumCount - 2) {
                        data.add(String.valueOf(position + 2));
                        mAdapter.notifyDataSetChanged();
                        mLinearLayoutManager.scrollToPositionWithOffset(position, 0);
                        mSvMain.post(new Runnable() {
                            @Override
                            public void run() {
                                mSvMain.smoothScrollTo(0, (int) (((DeviceParams.screenHeight(mContext) - mAdapter.getToolbarHeight()) / 3f) * (mMiddlePosition + 1) + mAdapter.getToolbarHeight()));
                            }
                        });
                    }
                }
            }
        });
    }

    private class ScrolledAdapter extends RecyclerView.Adapter<ScrolledAdapter.ScrolledViewHolder> {

        private Context mContext;
        private DisplayMetrics mDisplayMetrics;
        private float mToolbarHeight;
        private List<String> mData;
        private int mLastPosition = 2;
        private SimpleAdapter.OnItemClickListener onItemClickListener;

        public ScrolledAdapter(Context context, List<String> data) {
            this.mContext = context;
            this.mData = data;
            mDisplayMetrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(mDisplayMetrics);
            TypedValue typedValue = new TypedValue();
            mContext.getTheme().resolveAttribute(android.support.design.R.attr.actionBarSize, typedValue, true);
            mToolbarHeight = typedValue.getDimension(mDisplayMetrics) + getResources().getDimensionPixelSize(R.dimen.status_bar_height) + DeviceParams.dip2px(mContext, 3f);
        }

        @NonNull
        @Override
        public ScrolledViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ScrolledViewHolder(LayoutInflater.from(mContext).inflate(R.layout.c_layout_textview, parent, false));
        }

        @Override
        public void onBindViewHolder(@NonNull final ScrolledViewHolder holder, final int position) {
            holder.tvItem.setText(mData.get(position) + "  1312313213213132132132132132132132132132132132132132132133211111111111111111111111111111111111111111111111111111");
            holder.item.getLayoutParams().height = (int) ((DeviceParams.screenHeight(mContext) - mToolbarHeight) / 3f);

            holder.btnSubmit.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (onItemClickListener != null) {
                        onItemClickListener.onItemClick(v, position);
                    }
                }
            });

        }

        @Override
        public int getItemCount() {
            return mData.size();
        }

        public float getToolbarHeight() {
            return mToolbarHeight;
        }

        public void setOnItemClickListener(SimpleAdapter.OnItemClickListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
        }

        class ScrolledViewHolder extends RecyclerView.ViewHolder {

            private View item;
            private TextView tvItem;
            private Button btnSubmit;

            public ScrolledViewHolder(View itemView) {
                super(itemView);
                item = itemView.findViewById(R.id.ll_item);
                tvItem = (TextView) itemView.findViewById(R.id.tv_item);
                btnSubmit = (Button) itemView.findViewById(R.id.btn_submit);
            }
        }
    }
}
